﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using NhaThuocOnline.Entities;
using NhaThuocOnline.Repositories;
using NhaThuocOnline.ViewModels;

namespace NhaThuocOnline.Service
{
    public class DonViTinhService
    {
        public void InsertOrUpdate(ViewModelsDonViTinh dvt)
        {
            var en = Mapper.Map<ViewModelsDonViTinh, DonViTinh>(dvt);
            using (DonViTinhRepository repo = new DonViTinhRepository())
            {
                repo.InsertOrUpdate(en);
                repo.Save();
            }
        }
        public void Delete(int id)
        {
            using (DonViTinhRepository repo = new DonViTinhRepository())
            {
                repo.Delete(id);
                repo.Save();
            }
        }
        public List<ViewModelsDonViTinh> GetAll()
        {
            using (DonViTinhRepository repo = new DonViTinhRepository())
            {
                return Mapper.Map<List<DonViTinh>, List<ViewModelsDonViTinh>>(repo.GetAll.ToList());
            }
        }
        public ViewModelsDonViTinh GetDonViTinhById(int id)
        {
            using (DonViTinhRepository repo = new DonViTinhRepository())
            {
                return Mapper.Map<DonViTinh, ViewModelsDonViTinh>(repo.GetAll.Where(bn => bn.Id == id).FirstOrDefault());
            }
        }
    }
}
